<?php

class celebrityComponents extends sfComponents
{
    /**
     * 鍚屽睘鍏徃
     * param recordCount 淇℃伅鏉℃暟
     * param companyid   鍏徃id
     * @return 鍚屽叕鍙哥殑鏄庢槦淇℃伅
     */
    public function executeSamecompany() //Scl
    {
        /*$sql="select top 6 * from Celebrity where company_id=(select company_id from Celebrity where
        celebrity_id=".$celebrity_id.") and celebrity_id!=".$celebrity_id." order by create_time desc";*/
        $record_Count = 6;
        if (!empty($this->recordCount))
            $record_Count = $this->recordCount;
        if (empty($this->celebrityId))
            $this->celebrityId = "1000";
        $samComp = Doctrine_Core::getTable("celebrity")->find($this->celebrityId);

        //edited by sam 2011-03-24
        $companyName = $samComp->getCompanyName();

        if (!$companyName) $companyName = 'notexist';
        $companys = split('[,，]', $companyName);


        $execsql = Doctrine_Core::getTable("celebrity")->createQuery('c')->where('c.status_id=1005')->andWhere("c.company_name='" . str_replace("'", "''", $companyName) . "'")
                ->andWhere('celebrity_id !=' . $this->celebrityId)->limit($record_Count);

        //edited by sam 2011-03-24
        /*
         if(!empty($this->companyId)){
            $company_id=$this->companyId;
            $execsql=$execsql->where("company_id=".$company_id);
         }
         */
        $this->sclCelebritys = $execsql->orderBy("c.publish_time asc")->execute();
    }


    /**
     * 鏄庢槦棣栭〉閾炬帴鍒楄〃
     * @return void
     */
    public function executeRelated() //Ahl
    {
        $record_Count = 2;
        $artist = "";
        $tempK = urldecode($this->keyword);
        $tempK = preg_split("/，|,/", $tempK);
        $nbsps = 0;
        for ($i = 0; $i < sizeof($tempK); $i++)
        {
            if (!empty($tempK[$i]))
                $nbsps++;
            $tempN = str_replace("'", "''", $tempK[$i]);
            if (1 == $nbsps)
                $artist = " name = '" . $tempN . "' ";
            else if ($nbsps > 1) {
                if (!empty($tempN))
                    $artist .= " or name = '" . $tempN . "' ";
            }
        }
        if (empty($artist))
            $artist = "1=2";

        $execsql = Doctrine_Core::getTable("celebrity")->createQuery('c')
                ->where($artist)->andWhere('c.status_id=1005')->orderBy('c.publish_time desc')
                ->limit($record_Count);

        $this->celebrities = $execsql->execute();
    }

    public function executeCsl()
    {
        $this->cslCelebritys = Doctrine_Core::getTable("Celebrity")->createQuery('c')->limit(8)->execute();
    }

    /*
     * 瀹樻柟缃戠珯
     */
    public function executeOfficialsite() //Osl
    {
        if (null == $this->celebrityId || "" == $this->celebrityId)
            $this->celebrityId = 1000;
        $this->Officials = Doctrine_Core::getTable("OfficialSite")->createQuery('o')
                ->where('o.celebrity_id=' . $this->celebrityId)->execute();
    }

    public function executeStartouch() //Stl
    {
        $this->stlCelebritys = Doctrine_Core::getTable("Celebrity")
                ->createQuery('c')->where('c.status_id=1005')
                ->andWhere('is_recommend_b=1')
                ->addOrderBy('c.is_on_top_b desc')
                ->addOrderBy('c.publish_time desc')
                ->addOrderBy('c.celebrity_id desc')
                ->limit(8)
                ->execute();
    }

    public function executePrefer() //pcl
    {
        //$this->Celebritys=Doctrine_Core::getTable("Celebrity")->createQuery('c')->where('c.status_id=1005')->limit(5) ->execute();
        //        if (empty($this->celebrityId))
        //            $this->celebrityId = 1000;
        //        $this->Celebritys = array();
        //        $sqlStr = "select  * from Celebrity where celebrity_id in(select r.r_content_id from Celebrity c inner join RelatedContent r on c.celebrity_id=r.content_id
        //        where r.content_type_i=1002 and r.r_content_type_i=1002 and r.content_id=" . $this->celebrityId . ") and status_id=1005 order by (hit_i+hit_mask_i)desc limit 5";
        //        $connection = Doctrine_Manager::connection();
        //        $STH = $connection->execute($sqlStr);
        //        $STH->setFetchMode(PDO::FETCH_ASSOC);
        //        $i = 0;
        //        while ($row_array = $STH->fetch()) {
        //            $currentCele = array(
        //                "snapshot_big" => $row_array["snapshot_big"],
        //                "celebrity_id" => $row_array["celebrity_id"],
        //                "name" => $row_array["name"],
        //                "catalog_id" => $row_array["catalog_id"]
        //            );
        //            $this->Celebritys[] = $currentCele;
        //            $i++;
        //        }
        if (empty($this->celebrityId))
            $this->celebrityId = 1000;
        $celebrity = Doctrine_Core::getTable("celebrity")->find($this->celebrityId);
        $catalogId = $celebrity->getCatalogId();
        $ctype = $celebrity->getCType();
        $sex = $celebrity->getSex();
        $Celebritys = Doctrine_Core::getTable('celebrity')->createQuery('c')
                ->addWhere('c.catalog_id=?', $catalogId)->addWhere('c.status_id=?',1005)->addWhere('c.c_type=?', $ctype)->addWhere('c.celebrity_id!=?', $celebrity->getCelebrityId());
        if ($ctype == 1) { //单人
            $Celebritys = $Celebritys->addWhere('c.sex=?', $sex);
        }
        $this->Celebritys = $Celebritys->addOrderBy('c.publish_time desc')->limit(6)->execute();

    }

    public function executeList() //OSL
    {
        // $this->cvlCelebritys=Doctrine_Core::getTable("Celebrity")->createQuery('c')->limit(8) ->execute() ;
    }

    public function executeRecommend() //OSL
    {
        $counts = 8;
        if (!empty($this->recordCount))
            $counts = $this->recordCount;
        $this->chinCelebritys = Doctrine_Core::getTable("Celebrity")->createQuery('c')->where('catalog_id=1000')
                ->andWhere('c.status_id=1005')->andWhere('is_recommend_b=1')->addOrderBy('c.publish_time desc')
                ->addOrderBy('c.celebrity_id desc')->limit($counts)->execute();
        $this->kpopCelebritys = Doctrine_Core::getTable("Celebrity")->createQuery('c')->where('catalog_id=1001')
                ->andWhere('c.status_id=1005')->andWhere('is_recommend_b=1')->addOrderBy('c.publish_time desc')
                ->addOrderBy('c.celebrity_id desc')->limit($counts)->execute();
        $this->jpopCelebritys = Doctrine_Core::getTable("Celebrity")->createQuery('c')->where('catalog_id=1002')
                ->andWhere('c.status_id=1005')->andWhere('is_recommend_b=1')->addOrderBy('c.publish_time desc')
                ->addOrderBy('c.celebrity_id desc')->limit($counts)->execute();
        $this->westCelebritys = Doctrine_Core::getTable("Celebrity")->createQuery('c')->where('catalog_id=1003')
                ->andWhere('c.status_id=1005')->andWhere('is_recommend_b=1')->addOrderBy('c.publish_time desc')
                ->addOrderBy('c.celebrity_id desc')->limit($counts)->execute();
    }


    public function executeRelateNews()
    {
        if (empty($this->celebrityId))
            $this->celebrityId = 1000;
        $celebrity = Doctrine_Core::getTable("celebrity")->find($this->celebrityId);
        $tag = $celebrity->getTag();
        if ($tag == null || $tag == '') return;
        $tags = split('[,，]', $tag);
        $relateNews = Doctrine_Core::getTable('news')->createQuery('n');

        $this->buildSplitQuery($tag, "n.tag", $relateNews);
        //        foreach ($tags as $tagname) {
        //            $relateNews = $relateNews->orWhere("n.tag like '%" . $tagname . "%'");
        //        }
        $relateNews = $relateNews->andWhere('n.status_id=?', 1005)->addOrderBy('n.publish_time desc');
        $dql = $relateNews->getDql();
        $this->relateNews = $relateNews->limit(5)->execute();
    }

    public function executeRelatePics()
    {
        if (empty($this->celebrityId))
            $this->celebrityId = 1000;
        $celebrity = Doctrine_Core::getTable("celebrity")->find($this->celebrityId);
        $tag = $celebrity->getTag();
        if ($tag == null || $tag == '') return;

        $relatePics = Doctrine_Core::getTable('photogroup')->createQuery('p');

        $this->buildSplitQuery($tag, "p.tag", $relatePics);
        //        foreach ($tags as $tagname) {
        //            $relatePics = $relatePics->orWhere("p.tag like '%" . $tagname . "%'");
        //        }
        $relatePics = $relatePics->andWhere('p.status_id=?', 1005)->addOrderBy('p.publish_time desc');
        $this->relatePics = $relatePics->execute();
    }

    public function executeRelateVideos()
    {
        if (empty($this->celebrityId))
            $this->celebrityId = 1000;
        $celebrity = Doctrine_Core::getTable("celebrity")->find($this->celebrityId);
        $tag = $celebrity->getTag();
        if ($tag == null || $tag == '') return;
        $tags = split('[,，]', $tag);

        $relateVideos = Doctrine_Core::getTable('video')->createQuery('v');
        $this->buildSplitQuery($tag, "v.tag", $relateVideos);
        //        foreach ($tags as $tagname) {
        //            $relateVideos = $relateVideos->orWhere("v.tag like '%" . $tagname . "%' ");
        //        }
        $relateVideos = $relateVideos->andWhere('v.status_id=?', 1005)->addOrderBy('v.publish_time desc');
        $this->relateVideos = $relateVideos->limit(3)->execute();
    }


    private function buildSplitQuery($data, $targetColName, Doctrine_Query $query)
    {
        if ($data == null || trim($data) == '') return;
        mb_regex_encoding('UTF-8');
        $dataArray = mb_split("[,，]", $data, -1);
        $q = $targetColName . " like ? ";
        $count = count($dataArray);
        for ($i = 0; $i < $count; $i++)
        {
            $dataArray[$i] = "%" . $dataArray[$i] . "%";
        }

        if ($count > 1) {
            $qa = array_fill(0, $count, $q);
            $qs = implode(" or ", $qa);
            $query = $query->addWhere("( " . $qs . " )", $dataArray);
        } elseif ($count == 1) {
            $query = $query->addWhere($q, $dataArray[0]);
        }

    }
}